package com.iflytek.autofly.setting.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.iflytek.autofly.utils.FlyLog;

/**
 * @author 忧郁的西索 ★^_^+
 * @Description:SharedPreferences 操作工具
 * @date 2014-2-7 上午10:36:25
 */
public class SpUtil {
    private static final String TAG = "SpUtil调试日志";
    public static boolean logFlag = false;

    // [s] save
    public static void saveString(Context context, String spName, String key, String value) {
        if (logFlag) {
            FlyLog.d(TAG, "方法saveString :" + "context:" + context + "||spName:" + spName + "||key:" + key + "||value:"
                    + value);
        }

        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            Editor editor = sp.edit();
            editor.putString(key, value);
            editor.commit();
        }
    }

    public static void saveLong(Context context, String spName, String key, Long value) {
        if (logFlag) {
            FlyLog.d(TAG, "方法saveLong :" + "context:" + context + "||spName:" + spName + "||key:" + key + "||value:"
                    + value);
        }

        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            Editor editor = sp.edit();
            editor.putLong(key, value);
            editor.commit();
        }
    }

    public static void saveInt(Context context, String spName, String key, Integer value) {
        if (logFlag) {
            FlyLog.d(TAG, "方法saveInt :" + "context:" + context + "||spName:" + spName + "||key:" + key + "||value:"
                    + value);
        }

        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            Editor editor = sp.edit();
            editor.putInt(key, value);
            editor.commit();
        }
    }

    public static void saveBoolean(Context context, String spName, String key, Boolean value) {
        if (logFlag) {
            FlyLog.d(TAG, "方法saveBoolean :" + "context:" + context + "||spName:" + spName + "||key:" + key + "||value:"
                    + value);
        }

        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            Editor editor = sp.edit();
            editor.putBoolean(key, value);
            editor.commit();
        }
    }

    public static void saveFloat(Context context, String spName, String key, Float value) {
        if (logFlag) {
            FlyLog.d(TAG, "方法saveFloat :" + "context:" + context + "||spName:" + spName + "||key:" + key + "||value:"
                    + value);
        }

        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            Editor editor = sp.edit();
            editor.putFloat(key, value);
            editor.commit();
        }
    }

    // [e] save end

    // [s] get
    public static String getString(Context context, String spName, String key, String... defaultValue) {
        if (logFlag) {
            FlyLog.d(TAG, "方法getString :" + "context:" + context + "||spName:" + spName + "||key:" + key);
        }
        String defv = "";
        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            if (defaultValue != null && defaultValue.length != 0) {
                defv = defaultValue[0];
            }
            return sp.getString(key, defv);
        } else {
            return defv;
        }
    }

    public static Long getLong(Context context, String spName, String key, Long... defValue) {
        if (logFlag) {
            FlyLog.d(TAG, "方法getLong :" + "context:" + context + "||spName:" + spName + "||key:" + key);
        }

        Long defv = 0L;
        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            if (defValue != null && defValue.length != 0) {
                defv = defValue[0];
            }
            return sp.getLong(key, defv);
        } else {
            return defv;
        }
    }

    public static Integer getInt(Context context, String spName, String key, Integer... defValue) {
        if (logFlag) {
            FlyLog.d(TAG, "方法getInt :" + "context:" + context + "||spName:" + spName + "||key:" + key);
        }

        Integer defv = 0;
        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            if (defValue != null && defValue.length != 0) {
                defv = defValue[0];
            }
            return sp.getInt(key, defv);
        } else {
            return defv;
        }
    }

    public static Boolean getBoolean(Context context, String spName, String key, Boolean... defValue) {
        if (logFlag) {
            FlyLog.d(TAG, "方法getBoolean :" + "context:" + context + "||spName:" + spName + "||key:" + key);
        }

        Boolean defv = false;
        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            if (defValue != null && defValue.length != 0) {
                defv = defValue[0];
            }
            return sp.getBoolean(key, defv);
        } else {
            return defv;
        }
    }

    public static Float getFloat(Context context, String spName, String key, Float... defValue) {
        if (logFlag) {
            FlyLog.d(TAG, "方法getFloat :" + "context:" + context + "||spName:" + spName + "||key:" + key);
        }

        Float defv = 0f;
        if (!"".equals(spName) && context != null) {
            SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
            if (defValue != null && defValue.length != 0) {
                defv = defValue[0];
            }
            return sp.getFloat(key, defv);
        } else {
            return defv;
        }
    }
    // [e] get end
}
